# -*- coding: utf-8 -*-

"""
Unit tests for 'contact' application.

"""

from django.test import TestCase
from django.test.client import Client
from django.conf import settings
from django.core import mail

# Get the reverse function - If I have installed the localeurl application,
# this has its own custom reverse function.
try:
    from localeurl.models import reverse
except ImportError:
    from django.core.urlresolvers import reverse

import os

# Check that ReCaptcha is switched off.
assert(not settings.EXTERNAL_RECAPTCHA)

class BasicTest(TestCase):

    fixtures = ['contact_recaptcha_test_data.json']
    
    # The path to the 2 pages defined in this app can change according to the site.
    # Work out the absolute paths for the test suite.
    PathToContact = reverse('contact_recaptcha_contact')
    PathToThanks = reverse('contact_recaptcha_thanks')
    
    
    def test_contact_page(self):
        """Get contact page."""

        response = self.client.get(self.PathToContact)
        # Check that the response is 200
        self.assert_(response.status_code == 200, response.status_code)

        # Check that the expected HTML is present.
        self.assert_('<input id="id_subject" type="text"' in response.content, response.content)
        self.assert_('<textarea id="id_message"' in response.content)
        self.assert_('id="id_email' in response.content)
        self.assert_('ReCaptcha widget (dev version)' in response.content)

    def test_good_message(self):
        """Send valid message, get redirected to thanks page."""
        response = self.client.post(self.PathToContact, {'subject': 'hello',
                                                  'message':'Just a quick hello.'})
        self.assertRedirects(response, self.PathToThanks)

        # Check that an email has been sent and is sitting in the test framework's outbox
        self.assert_(len(mail.outbox) == 1)
        self.assert_(mail.outbox[0].subject == 'hello', mail.outbox[0].subject)
        self.assert_('Just a quick hello.' in mail.outbox[0].body, mail.outbox[0].body)

    def test_good_message2(self):
        """Send valid message (with an email address), get redirected to thanks page."""
        response = self.client.post(self.PathToContact, {'subject': 'hello',
                                                  'message':'Just a quick hello.',
                                                  'email': 'test@hotmail.com'})
        self.assertRedirects(response, self.PathToThanks)

    def test_no_subject(self):
        """Missing subject line."""
        response = self.client.post(self.PathToContact, {'subject': '',
                                                  'message':'Just a quick hello.'})

        # Get an error back with the form.
        self.assert_(response.status_code == 200, response.status_code)
        self.assert_(response.context[0]['form'].errors)
        self.assert_('This field is required' in response.content)

    def test_preset_subject(self):
        """Preset the subject line."""
        response = self.client.get(self.PathToContact, {'subject': 'This is the subject'})
        self.assert_('value="This is the subject"' in response.content)

    def test_bad_email(self):
        """Invalid email."""
        response = self.client.post(self.PathToContact, {'subject': '',
                                                  'message':'Just a quick hello.',
                                                  'email': 'tttttt'})

        # Get an error back with the form.
        self.assert_(response.status_code == 200, response.status_code)
        self.assert_(response.context[0]['form'].errors)
        self.assert_('Enter a valid e-mail address' in response.content)

    def test_logged_in(self):
        """Logged in users do not see the captcha."""
        
        self.client.login(username='contactrecaptcha', password='duck')
        response = self.client.get(self.PathToContact)
        self.assert_('Please also type the following 2 words' not in response.content)

    def test_thanks_page(self):
        """Get thanks page."""

        response = self.client.get(self.PathToThanks)
        # Check that the response is 200
        self.assert_(response.status_code == 200, response.status_code)

        # Check that the expected HTML is present.
        self.assert_('Thank you' in response.content, response.content)

    def test_sitemap(self):
        """Check that the contact pages do not appear in the sitemap - they have no added value."""

        response = self.client.get('/sitemap.xml')
        
        # Not all sites will define a sitemap.
        if response.status_code != 404:
            self.assert_(response.status_code == 200, response)
    
            self.assert_('/contact/</loc>' not in response.content, response)
            self.assert_('/contact/thanks/</loc>' not in response.content, response)





